$(function () {
    $(".addVersion").on("click",function(){
        var obj = $(this);
        window.location.href="/appmanager/version/add?appId="+obj.attr("appId");
    });
    $(".modifyVersion").on("click",function(){
        var obj = $(this);
        var status = obj.attr("status");
        var versionCode = obj.attr("versionCode");
        var appId = obj.attr("appId");
        if(status == 0 || status == 4){//待审核、审核未通过状态下才可以进行修改操作
            window.location.href="update?appId="+appId;
        }else{
            alert("待审核、审核未通过状态下才可以进行修改操作！");
        }
    });
    $(".modifyAppInfo").on("click",function(){
        var obj = $(this);
        var status = obj.attr("status");
        var appId = obj.attr("appId");
        if(status == 0 || status == 4){//待审核、审核未通过状态下才可以进行修改操作
            window.location.href="/appmanager/app/update?appId="+appId;
        }else{
            alert("待审核、审核未通过状态下才可以进行修改操作！");
        }
    });
});


$(document).on("click",".saleSwichOpen,.saleSwichClose",function(){
    var obj=$(this);
    var appId = obj.attr("appId");
    var saleSwitch = obj.attr("saleSwitch");
    if("open" === saleSwitch){
        saleSwitchAjax(appId,saleSwitch,obj);
    }else if("close" === saleSwitch){
        if(confirm("你确定要下架您的APP应用【"+obj.attr("appName")+"】吗？")){
            saleSwitchAjax(appId,saleSwitch,obj);
        }
    }
});

var saleSwitchAjax = function(appId,saleSwitch,obj){
    $.getJSON({
        url:"shelf",
        dataType:"json",
        data:{
            "appId":appId,
            "todo":saleSwitch
        },
        success:function(data){
			/*
			 * resultMsg:success/failed
			 * errorCode:exception000001
			 * appId:appId
			 * errorCode:param000001
			 */
            if(data.errorCode == 0){
                if(data.resultMsg === "success"){//操作成功
                    if("open" === obj.attr("saleSwitch")){
                        alert("恭喜您，【"+obj.attr("appName")+"】的【上架】操作成功");
                        obj.parents('tr').find('#appStatus').html("已上架");
                        obj.className="saleSwichClose";
                        obj.html("下架");
                        obj.attr("saleSwitch","close");
                        obj.parents('tr').find('#appStatus').css({
                            'padding':'3px',
                            'border-radius':'3px'
                        });
                        // $("#appInfoStatus" + obj.attr("appinfoid")).hide();
                        // $("#appInfoStatus" + obj.attr("appinfoid")).slideDown(300);
                    }else if("close" === obj.attr("saleSwitch")){
                        alert("恭喜您，【"+obj.attr("appName")+"】的【下架】操作成功");
                        obj.parents('tr').find('#appStatus').html("已下架");
                        obj.className="saleSwichOpem";
                        obj.html("上架");
                        obj.attr("saleSwitch","open");
                        obj.parents('tr').find('#appStatus').css({
                            'padding':'3px',
                            'border-radius':'3px'
                        });
                        // $("#appInfoStatus" + obj.attr("appinfoid")).hide();
                        // $("#appInfoStatus" + obj.attr("appinfoid")).slideDown(300);
                    }
                }else if(data.resultMsg === "failed"){//操作失败
                    if("open" === obj.attr("saleSwitch")){
                        alert("【"+obj.attr("appsoftwarename")+"】的【上架】操作失败");
                    }else if("close" === obj.attr("saleSwitch")){
                        alert("【"+obj.attr("appsoftwarename")+"】的【下架】操作失败");
                    }
                }
            }else{
                if(data.errorCode === 'exception000001'){
                    alert("对不起，系统出现异常，请联系IT管理员");
                }else if(data.errorCode === 'param000001'){
                    alert("对不起，参数出现错误，您可能在进行非法操作");
                }
            }
        },
        error:function(data){
            alert("未知错误");
        }
    });
};



	
